<?php

namespace App\Http\Controllers\Admin;

use App\Repository\Order\HistoryOrderRepository as model;
use Illuminate\Http\Request;

// 分销商
class OrderHistoryController extends BaseController
{
  public function __construct(model $model)
  {
    $this->repoist = $model;
  }
  public function index(Request $request)
  {
    $where = [];
    if (!empty($request->get('status'))) {
      $where['where']['status'] = $request->get('status');
    }
    if (!empty($request->get('user')->role_id) && $request->get('user')->role_id != 1 && empty($request->get('is_dist'))) {
      $where['where']['shop_id'] = $request->get('user')->shop_id;
    }
    if (!empty($request->get('is_dist'))) {
      $where['like']['shop_str'] = $request->get('user')->shop_id;
    }
    if (!empty($request->get('shop_id'))) {
      $where['like']['shop_id_str'] = $request->get('user')->shop_id . '.' . $request->get('shop_id');
    }
    if (!empty($request->get('orderno'))) {
      $where['where']['orderno'] = trim($request->get('orderno'), ' ');
    }
    if (!empty($request->get('pay_identity'))) {
      $where['where']['pay_identity'] = trim($request->get('pay_identity'), ' ');
    }
    if (!empty($request->get('sup_orderno'))) {
      $where['where']['sup_orderno'] = $request->get('sup_orderno');
      // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    }
    if (!empty($request->get('dist_orderno'))) {
      $where['where']['order_num'] = $request->get('dist_orderno');
      // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    }
    if (!empty($request->get('iccid'))) {
      $where['where']['iccid'] = $request->get('iccid');
      // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    }
    if (!empty($request->get('express_number'))) {
      $where['where']['express_number'] = $request->get('express_number');
      // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    }
    if (!empty($request->get('contact'))) {
      $where['where']['user_phone'] = $request->get('contact');
      // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    }
    if (!empty($request->get('name'))) {
      $where['where']['user_name'] = $request->get('name');
      // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    }
    if (!empty($request->get('number'))) {
      $where['like']['name'] = $request->get('number');
    }
    //时间验证 
    // if (!empty($request->get('dateRange'))) {
    //   // $time = $request->get('dateRange');
    //   $where['between'] = ['created_at' => $request->get('dateRange')];
    // }
    // var_dump($where);
    // if (empty($where)) {
    //   return $this->returnMsg(200, '查询成功', []);
    // }
    $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
    $where['limit']['number'] = $request->get('pageSize');

    $data =  $this->repoist->index($where);
    return $data;
  }
}
